package com.smartleanx.module.hrm.service.esign.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartleanx.module.hrm.dal.mysql.esign.HrmEContractTempActorsMapper;
import com.smartleanx.module.hrm.service.esign.service.HrmEContractTempActorsService;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEContractTempActors;
import com.smartleanx.module.hrm.service.esign.temp.vo.HrmEContractTempActorsVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * e签报模板参与者Service实现
 *
 * @author dongbidongxin
 * @since 2025-07-07 15:56:14
 */
@Service
public class HrmEContractTempActorsServiceImpl extends ServiceImpl<HrmEContractTempActorsMapper, HrmEContractTempActors> implements HrmEContractTempActorsService {


    @Override
    public int saveTempActor(List<HrmEContractTempActorsVo> list,Long tempId) {
        List<HrmEContractTempActors> resultList = list.stream().map(p -> {
            HrmEContractTempActors hrmEContractTempActors = new HrmEContractTempActors();
            BeanUtil.copyProperties(p, hrmEContractTempActors);
            hrmEContractTempActors.setId(null);
            hrmEContractTempActors.setDataStatus(1);
            hrmEContractTempActors.setTempId(tempId);
            return hrmEContractTempActors;
        }).collect(Collectors.toList());

        saveBatch(resultList);
        return resultList.size();
    }

    @Override
    public List<HrmEContractTempActors> getTempActorsByTempId(Long tempId) {
        LambdaQueryWrapper<HrmEContractTempActors> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HrmEContractTempActors::getDataStatus,1);
        queryWrapper.eq(HrmEContractTempActors::getTempId,tempId);
        return list(queryWrapper);
    }

    @Override
    public List<HrmEContractTempActors> getTempActorsByTempId(Long tempId, boolean fillInSort) {
        LambdaQueryWrapper<HrmEContractTempActors> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HrmEContractTempActors::getDataStatus,1);
        queryWrapper.eq(HrmEContractTempActors::getTempId,tempId);
        queryWrapper.orderByAsc(fillInSort,HrmEContractTempActors::getActorFillInSort);
        return list(queryWrapper);
    }

    @Override
    public boolean removeActorsByTempId(Long tempId) {
        return update(new LambdaUpdateWrapper<HrmEContractTempActors>()
        .eq(HrmEContractTempActors::getDataStatus,1)
        .eq(HrmEContractTempActors::getTempId,tempId)
        .set(HrmEContractTempActors::getDataStatus,0));
    }
}
